package edu.lambdateacher.awt;

/*
 LambdaTeacher
 Copyright (C) 2000  Kim Mason
 Copyright (C) 2014  Vitalij Zadneprovskij

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import edu.lambdateacher.expressions.Expression;

/**
 * The class ChooseComponent is a component which is used to choose sub
 * expressions of an expression.
 */
public class ChooseComponent extends Canvas {
	private static final long serialVersionUID = 6983872286748874733L;

	private FontMetrics curfontmetrics = null;
	private Font curfont = null;
	private Expression theexpr = null;
	private Expression outexpr = null;
	private List<ExpressionRepresentation> rectvector = null;
	private String todraw = null;
	private Dimension size = null;
	private Rectangle tohighlight = null;

	// A simple even handler for mouseclicks
	private MouseAdapter thismouseadaptor = new MouseAdapter() {
		public void mouseClicked(MouseEvent e) {
			// create an enumeration of all elements
			boolean rectselect = false;
			// check each element to see if its rect contains the
			// clickpoint
			for (ExpressionRepresentation curelement : rectvector) {
				Point where = e.getPoint();

				if (curelement.surroundrect.contains(where)) {
					tohighlight = curelement.expressionrect;
					outexpr = curelement.theexpr;
					rectselect = true;
					break;
				}
			}
			// if nothing was selected, clear the highlight
			if (rectselect == false) {
				tohighlight = null;
				outexpr = null;
			}
			ChooseComponent.this.update(ChooseComponent.this.getGraphics());
		}
	};

	public ChooseComponent(Expression inexpr) {
		super();
		// add event handlers
		this.addMouseListener(thismouseadaptor);
		// set a font to use.
		curfont = new Font("Monospaced", Font.PLAIN, 14);
		// use it
		this.setFont(curfont);
		// get the fontmetrics for that font
		curfontmetrics = this.getFontMetrics(curfont);

		theexpr = inexpr;
		// create a new dummy element for the vector
		ExpressionRepresentation starter = new ExpressionRepresentation();
		// create the vector of ExprRep's
		rectvector = new ArrayList<ExpressionRepresentation>();
		// add the dummy starting element
		rectvector.add(starter);
		// fill in the vector of expression rects
		//theexpr.unParseExprRep(rectvector, curfontmetrics);


		todraw = "";
		for (ExpressionRepresentation curelement : rectvector) {
			todraw = todraw + curelement.toshow;
		}

		// now set the size of the current component
		int width = curfontmetrics.stringWidth(todraw);
		int height = curfontmetrics.getHeight();
		size = new Dimension(width, height);
		this.setSize(size);
	}

	/*
	 * getExpr simply returns the currently selected expression
	 */
	public Expression getExpr() {
		return this.outexpr;
	}

	public Dimension getMinimumSize() {
		return size;
	}

	public Dimension getPreferredSize() {
		return size;
	}

	/*
	 * paint overrides the paint method in the Canvas class. It simply draws the
	 * string todraw at the point 0,0, and xors the selected rectangle if it is
	 * selected.
	 */
	public void paint(Graphics g) {
		g.drawString(todraw, 0, curfontmetrics.getAscent());
		if (tohighlight != null) {
			Color oldcolor = g.getColor();
			g.setXORMode(Color.white);
			g.fillRect(tohighlight.x, tohighlight.y, tohighlight.width, tohighlight.height);
			g.setPaintMode();
		}
	}

}
